package sh.wn36.vehiclemanage.xzh;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sh.wn36.vehiclemanage.entity.FeesManager;
import sh.wn36.vehiclemanage.service.*;
import sh.wn36.vehiclemanage.service.impl.CarServiceImpl;
import sh.wn36.vehiclemanage.util.Result;

import java.util.List;

@Api("规费管理")
@CrossOrigin
@RestController
@RequestMapping("/fees")
public class Fee_Manger_Controller {
    @Autowired
    private CarServiceImpl carService;
    @Autowired
    private CurrentUnitService currentUnitService;
    @Autowired
    private QUsersService qUsersService;
    @Autowired
    private FeesManagerService feesManagerService;

    @ApiOperation("获取规费记录列表")
    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result getFeesList(@ApiParam("当前页码") @PathVariable("currentPage") Integer currentPage,
                                      @ApiParam("每页大小") @PathVariable("pageSize") Integer pageSize,
                                      @ApiParam("模糊查询内容")@RequestBody(required = false) FeeQuery feeQuery
    ) {
        Page<RepairInfo> page = new Page<>(currentPage, pageSize);
        IPage<FeeInfo> feeInfoIPage = null;
        if (feeQuery.getSelect() != null) {
            Integer select = feeQuery.getSelect();
            String text = feeQuery.getRecordInfo();
            switch (select) {
                case 1:
                    feeInfoIPage = feesManagerService.getListByCarNo(page, text);
                    break;
                case 2:
                    feeInfoIPage = feesManagerService.getListByUnitName(page, text);
                    break;
                default:
                    feeInfoIPage = feesManagerService.getListByRname(page, text);
                    break;
            }
        } else {
            feeInfoIPage = feesManagerService.getList(page);
        }
        long total = page.getTotal();
        List<FeeInfo> feeInfoList = feeInfoIPage.getRecords();
        return Result.OK().data("total", total).data("list", feeInfoList);
    }

    @ApiOperation("获取所有的车牌号，修车厂，经办人")
    @GetMapping("/getList")
    public Result getAddList() {
        return Result.OK().data("carList", carService.list()).data("unitList", currentUnitService.list()).data("usersList", qUsersService.list());
    }

    @ApiOperation("保存规费记录")
    @PostMapping("/save")
    public Result saveOrUpdate(@ApiParam("前端获取的数据") @RequestBody(required = true) FeesManager feesManager) {
        boolean save = feesManagerService.save(feesManager);
        if (save){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @ApiOperation("修改规费记录")
    @PostMapping("/update")
    public Result update(@ApiParam("前端获取的数据") @RequestBody(required = true) FeesManager feesManager) {
        boolean b = feesManagerService.updateById(feesManager);
        if (b){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }


    @ApiOperation("根据Id获取规费信息")
    @GetMapping("/getById/{id}")
    public Result getById(@ApiParam("前端获取的Id") @PathVariable("id") Integer id) {

        return Result.OK().data("feesRecord", feesManagerService.getById(id));
    }


    @ApiOperation("根据id删除信息")
    @GetMapping("/remove/{id}")
    public Result remove(@ApiParam("根据id删除维修记录") @PathVariable("id") Integer id) {
        boolean b = feesManagerService.removeById(id);
        if (b){
            return Result.OK();
        }else {
            return Result.Fail();
        }
    }

    @ApiOperation("报表接口")
    @GetMapping("/table")
    public Result table(){
        return Result.OK().data("table",feesManagerService.getTable());
    }


    @ApiOperation("根据车牌Id报表接口")
    @GetMapping("/tableByCarId/{id}")
    public Result tableByCarId(@ApiParam("车牌的id")@PathVariable("id")Integer id){
        return Result.OK().data("table",feesManagerService.getTableByCarId(id));
    }
}
